﻿using XinLife.Application.Interface;
using XinLife.Common;
using XinLife.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using XinLife.Core.Model;

namespace XinLife.Controllers.Admin
{
    /// <summary>
    /// 数据字典
    /// </summary>
    public class DataItemController : BaseController
    {
        IDataItemService _dataItemService;
        public DataItemController(IDataItemService dataItemService)
        {
            _dataItemService = dataItemService;
        }
        /// <summary>
        /// 获取字典列表
        /// </summary>
        [HttpGet]
        public async Task<object> GetDataItemPageList([FromQuery] Pagination pagination, [FromQuery] string queryJson = null)
        {
            return await _dataItemService.GetDataItemPageList(pagination, queryJson);
        }
        /// <summary>
        /// 获取分类
        /// </summary>-
        /// <returns></returns>
        [HttpGet]
        public async Task<AjaxResult> GetDataItemType()
        {
            return await _dataItemService.GetDataItemType();
        }
        /// <summary>
        /// 获取菜单表单信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AjaxResult> GetDataItemForm(string id)
        {
            return await _dataItemService.GetDataItemForm(id);
        }
        /// <summary>
        /// 编辑分类
        /// </summary>-
        /// <returns></returns>
        [HttpPost]
        public async Task<AjaxResult> EditDataItemType([FromForm] DataItem dataItem)
        {
            return await _dataItemService.EditDataItemType(dataItem);
        }
        /// <summary>
        /// 修改字典
        /// </summary>-
        /// <returns></returns>
        [HttpPost]
        public async Task<AjaxResult> EditDataItem([FromForm] DataItem dataItem)
        {
            return await _dataItemService.EditDataItem(dataItem);
        }
        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public async Task<AjaxResult> DelDataItem([FromForm] string ids)
        {
            return await _dataItemService.DelDataItem(ids);
        }
        /// <summary>
        /// 获取所有字典
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AjaxResult> GetAllDataItems()
        {
            var data = await _dataItemService.GetAllDataItems();
            Func<List<DataItem>, string, List<object>> res = null;
            res = (List<DataItem> list, string parentid) =>
            {
                var items = list.FindAll(t => t.cParentId == parentid).ToList();

                var b = new List<object>();

                items.ForEach(t =>
                {
                    b.Add(new
                    {
                        Id = t.cId,
                        spread = true,
                        ParentId = parentid,
                        Title = parentid == "0" ? t.cItemTitle : t.cItemName,
                        Children = res(list, t.cId)
                    });
                });
                return b;
            };
            return Success(res(data, "0"));
        }

        /// <summary>
        /// 获取字典
        /// </summary>
        [HttpGet]
        public async Task<AjaxResult> GetDataItemByCode(string code)
        {
            return await _dataItemService.GetDataItemByCode(code);
        }
    }
}
